<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="深度学习——图像分类入门PartⅡ, J Sir">
    <meta name="description" content="">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>深度学习——图像分类入门PartⅡ | J Sir</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 6.0.0">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">J Sir</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">J Sir</div>
        <div class="logo-desc">
            
            Never really desperate, only the lost of the soul.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/jy741" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/jy741" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/10.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">深度学习——图像分类入门PartⅡ</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/python/">
                                <span class="chip bg-color">python</span>
                            </a>
                        
                            <a href="/tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">
                                <span class="chip bg-color">深度学习</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-10-28
                </div>
                

                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    5.3k
                </div>
                

                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/libs/prism/prism.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h1 id="EverydayOneCat"><a href="#EverydayOneCat" class="headerlink" title="EverydayOneCat"></a>EverydayOneCat</h1><p>“什么时候?” “猫!”</p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/ca992b8d5227e9d6a46eba7d9337e167d98df0a4.jpg@1036w.webp" alt="img" style="zoom:50%;"></p>
<span id="more"></span>
<h1 id="MobileNet"><a href="#MobileNet" class="headerlink" title="MobileNet"></a>MobileNet</h1><p>传统卷积神经网络，内存需求大、运算量大，导致无法在移动设备以及嵌入式设备上运行。</p>
<h2 id="MobileNet-V1"><a href="#MobileNet-V1" class="headerlink" title="MobileNet  V1"></a>MobileNet  V1</h2><p>MobileNet网络是由google团队在2017年提出的，专注于移动端或者嵌入式设备中的轻量级CNN网络。相比传统卷积神经网络，在准确率小幅降低的前提下大大减少模型参数与运算量。(相比VGG16准确率减少了0.9%，但模型参数只有VGG的1/32)</p>
<p>MobileNet V1网络中的亮点：</p>
<ul>
<li>Depthwise Convolution(大大减少运算量和参数数量)</li>
<li>增加超参数α、β</li>
</ul>
<h3 id="DW卷积"><a href="#DW卷积" class="headerlink" title="DW卷积"></a>DW卷积</h3><p>先来看我们传统的卷积</p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221025170632550.png" alt="image-20221025170632550"></p>
<p>传统卷积卷积核channel = 输入特征矩阵的channel 、 输出特征矩阵的channel = 卷积核个数</p>
<p>接下来我们来看DW卷积（Depthwise Conv）是什么</p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221025170942533.png" alt="image-20221025170942533"></p>
<p>每个卷积核的深度都是1，每个卷积核都只负责和输入特征矩阵的一个通道数进行计算，得到输出特征矩阵的一个channel，一个卷积核负责一个channel；所以卷积核的个数和输入特征矩阵的深度相同；输出特征矩阵的深度也就和卷积核的个数相同。</p>
<p>而我们MobileNet所用的是深度可分的卷积操作（depthwise separable Conv） ，由两部分组成：DW卷积+PW卷积</p>
<p>PW卷积就是卷积核大小为1*1的传统卷积</p>
<p><img src="C:\Users\Administrator\Desktop\学习\博客图片\image-20221025171214562.png" alt="image-20221025171214562" style="zoom:67%;"></p>
<p>我们来对比深度可分卷积和普通卷积计算量大小。</p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221025171452238.png" alt="image-20221025171452238"></p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221025171506633.png" alt="image-20221025171506633"></p>
<p>参数解读：<br>$D_F$：Input高宽大小<br>M：Input维度<br>$D_K$：卷积核大小（默认为3）<br>N：Output维度</p>
<p>传统卷积所需参数：$D_K<em>D_K</em>M<em>N</em>D_F*D_F$</p>
<p>深度可分卷积所需参数：$D_K<em>D_K</em>D_F<em>D_F</em>M+D_F<em>D_F</em>M*N$</p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221025171848416.png" alt="image-20221025171848416"></p>
<h3 id="MobileNet-V1结构分析"><a href="#MobileNet-V1结构分析" class="headerlink" title="MobileNet  V1结构分析"></a>MobileNet  V1结构分析</h3><p>V1网络整体结构：</p>
<p><img src="C:\Users\Administrator\Desktop\学习\博客图片\image-20221025172208920.png" alt="image-20221025172208920" style="zoom:80%;"></p>
<p>表中conv/s2代表普通卷积，stride=2，filter shape那一列中的高3 <em> 宽3 </em> 输入特征矩阵的深度3 * 卷积核个数32；</p>
<p>conv dw /s1对应DW卷积操作且stride=1，filter shape列中DW卷积的深度为1，所以他的卷积核的shape是高3<em> 宽3 </em> 卷积核深度1 * 输出特征矩阵深度32；</p>
<p>参照原论文表格可以看出计算量大幅减少：</p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221025172338126.png" alt="image-20221025172338126"></p>
<p>α超参数是卷积核个数的倍率，也就是控制卷积过程中卷积核的个数，alpha=1.0时，准确率为70.6%，alpha=0.75，也就是卷积核个数缩减为原来的0.75倍，准确率为68.4%；可以看出卷积核个数减少了不多，但是模型参数却减少了很多。 </p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221025172407718.png" alt="image-20221025172407718"></p>
<p>β是分辨率的参数，表格7看出不同的输入图像尺寸对于网络准确率和模型计算量的对比，224<em>224的RGB图像，计算准确率为70.6%，计算量是569million；192</em>192的RGB图像，计算准确率为69.1%，计算量是418；可以适当减少输入图像的大小来使得准确率降低一点的情况下，计算量大幅减少。 </p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221025172458839.png" alt="image-20221025172458839"></p>
<p>V1有一个很大的缺陷：depthwise部分的卷积核容易费掉，即卷积核参数大部分为零。由此V2应运而生。</p>
<h2 id="MobileNet-V2"><a href="#MobileNet-V2" class="headerlink" title="MobileNet  V2"></a>MobileNet  V2</h2><p>MobileNet v2网络是由google团队在2018年提出的，相比MobileNet V1网络，准确率更高，模型更小。</p>
<p>本网络的亮点：</p>
<ul>
<li>Inverted Residuals（倒残差结构）</li>
<li>Linear Bottlenecks</li>
</ul>
<h3 id="倒残差结构"><a href="#倒残差结构" class="headerlink" title="倒残差结构"></a>倒残差结构</h3><p><img src="C:\Users\Administrator\Desktop\学习\博客图片\image-20221025204431225.png" alt="image-20221025204431225" style="zoom:67%;"></p>
<p>如上图，左边是之前在ResNet中介绍的残差结构，它是先对输入特征矩阵采用1 × 1的卷积核来对特征矩阵进行压缩，也就是减少输入特征矩阵的channel，然后用3 × 3卷积核处理，最后用1 × 1的卷积核来扩充channel； </p>
<p>右边则是MobileNet V2新增的倒残差结构，先用1×1的卷积核进行升维操作，将channel变的更深，通过卷积核大小为3×3的DW操作进行卷积，再用1×1的卷积核进行降维。</p>
<p>倒残差结构是先用卷积升维，在用卷积降维；残差结构是反过来的，先用卷积降维，在用卷积升维。 </p>
<p>普通残差结构的激活函数为ReLU，倒残差结构的激活函数为ReLU6。（原因：ReLU激活函数对低维特征信息照成大量损失）</p>
<script type="math/tex; mode=display">
y=ReLu6(x)=min(max(x,0),6)</script><p><img src="C:\Users\Administrator\Desktop\学习\博客图片\image-20221025204806641.png" alt="image-20221025204806641" style="zoom:67%;"></p>
<p><img src="C:\Users\Administrator\Desktop\学习\博客图片\image-20221025205251530.png" alt="image-20221025205251530" style="zoom:80%;"></p>
<p>左边是Mobilenet V2倒残差网络结构图，通过1×1的卷积，ReLU6激活，3×3的DW卷积，ReLU6激活，1×1的卷积来处理，线性激活。对应右表给的每一层信息，第一层输入特征矩阵的高宽深度h×w×k，1×1卷积核进行升维处理，output里面的t是扩展因子，表示1×1卷积核的个数是tk个，输出特征矩阵的深度就是tk；第二层输入等于第一层输出，经过3×3的DW卷积核，步长为s，输出矩阵的深度和输入矩阵深度相同；第三层，采用1×1的卷积核降维，使用k’个卷积核。</p>
<blockquote>
<p>当stride=1且输入特征矩阵与输出特征矩阵shape相同才有shortcut连接</p>
</blockquote>
<h3 id="MobileNet-V2结构分析"><a href="#MobileNet-V2结构分析" class="headerlink" title="MobileNet  V2结构分析"></a>MobileNet  V2结构分析</h3><p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221025210424126.png" alt="image-20221025210424126"></p>
<h3 id="model"><a href="#model" class="headerlink" title="model"></a>model</h3><p>定义我们的基础卷积结构</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">ConvBNReLU</span><span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> in_channel<span class="token punctuation">,</span> out_channel<span class="token punctuation">,</span> kernel_size<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">,</span> stride<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> groups<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        padding <span class="token operator">=</span> <span class="token punctuation">(</span>kernel_size <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">//</span> <span class="token number">2</span>
        <span class="token builtin">super</span><span class="token punctuation">(</span>ConvBNReLU<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>in_channel<span class="token punctuation">,</span> out_channel<span class="token punctuation">,</span> kernel_size<span class="token punctuation">,</span> stride<span class="token punctuation">,</span> padding<span class="token punctuation">,</span> groups<span class="token operator">=</span>groups<span class="token punctuation">,</span> bias<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">(</span>out_channel<span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU6<span class="token punctuation">(</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>定义倒残差结构</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">InvertedResidual</span><span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Module<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> in_channel<span class="token punctuation">,</span> out_channel<span class="token punctuation">,</span> stride<span class="token punctuation">,</span> expand_ratio<span class="token punctuation">)</span><span class="token punctuation">:</span>  <span class="token comment">#expand_ratio就是扩展因子t</span>
        <span class="token builtin">super</span><span class="token punctuation">(</span>InvertedResidual<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        hidden_channel <span class="token operator">=</span> in_channel <span class="token operator">*</span> expand_ratio  <span class="token comment">#求出tk</span>
        self<span class="token punctuation">.</span>use_shortcut <span class="token operator">=</span> stride <span class="token operator">==</span> <span class="token number">1</span> <span class="token keyword">and</span> in_channel <span class="token operator">==</span> out_channel

        layers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
        <span class="token keyword">if</span> expand_ratio <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">:</span>
            <span class="token comment"># 1x1 pointwise conv</span>
            layers<span class="token punctuation">.</span>append<span class="token punctuation">(</span>ConvBNReLU<span class="token punctuation">(</span>in_channel<span class="token punctuation">,</span> hidden_channel<span class="token punctuation">,</span> kernel_size<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        layers<span class="token punctuation">.</span>extend<span class="token punctuation">(</span><span class="token punctuation">[</span>
            <span class="token comment"># 3x3 depthwise conv</span>
            ConvBNReLU<span class="token punctuation">(</span>hidden_channel<span class="token punctuation">,</span> hidden_channel<span class="token punctuation">,</span> stride<span class="token operator">=</span>stride<span class="token punctuation">,</span> groups<span class="token operator">=</span>hidden_channel<span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token comment"># 1x1 pointwise conv(linear)</span>
            nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>hidden_channel<span class="token punctuation">,</span> out_channel<span class="token punctuation">,</span> kernel_size<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> bias<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">(</span>out_channel<span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token punctuation">]</span><span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>conv <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span><span class="token operator">*</span>layers<span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">if</span> self<span class="token punctuation">.</span>use_shortcut<span class="token punctuation">:</span>
            <span class="token keyword">return</span> x <span class="token operator">+</span> self<span class="token punctuation">.</span>conv<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            <span class="token keyword">return</span> self<span class="token punctuation">.</span>conv<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>最后定义模型</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment">#返回ch值最接近divisor的整数倍</span>
<span class="token keyword">def</span> <span class="token function">_make_divisible</span><span class="token punctuation">(</span>ch<span class="token punctuation">,</span> divisor<span class="token operator">=</span><span class="token number">8</span><span class="token punctuation">,</span> min_ch<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    This function is taken from the original tf repo.
    It ensures that all layers have a channel number that is divisible by 8
    It can be seen here:
    https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
    """</span>
    <span class="token keyword">if</span> min_ch <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
        min_ch <span class="token operator">=</span> divisor
    new_ch <span class="token operator">=</span> <span class="token builtin">max</span><span class="token punctuation">(</span>min_ch<span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span>ch <span class="token operator">+</span> divisor <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">//</span> divisor <span class="token operator">*</span> divisor<span class="token punctuation">)</span>
    <span class="token comment"># Make sure that round down does not go down by more than 10%.</span>
    <span class="token keyword">if</span> new_ch <span class="token operator">&lt;</span> <span class="token number">0.9</span> <span class="token operator">*</span> ch<span class="token punctuation">:</span>
        new_ch <span class="token operator">+=</span> divisor
    <span class="token keyword">return</span> new_ch

<span class="token keyword">class</span> <span class="token class-name">MobileNetV2</span><span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Module<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> num_classes<span class="token operator">=</span><span class="token number">1000</span><span class="token punctuation">,</span> alpha<span class="token operator">=</span><span class="token number">1.0</span><span class="token punctuation">,</span> round_nearest<span class="token operator">=</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token builtin">super</span><span class="token punctuation">(</span>MobileNetV2<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        block <span class="token operator">=</span> InvertedResidual
        input_channel <span class="token operator">=</span> _make_divisible<span class="token punctuation">(</span><span class="token number">32</span> <span class="token operator">*</span> alpha<span class="token punctuation">,</span> round_nearest<span class="token punctuation">)</span>
        last_channel <span class="token operator">=</span> _make_divisible<span class="token punctuation">(</span><span class="token number">1280</span> <span class="token operator">*</span> alpha<span class="token punctuation">,</span> round_nearest<span class="token punctuation">)</span>

        inverted_residual_setting <span class="token operator">=</span> <span class="token punctuation">[</span>
            <span class="token comment"># t, c, n, s</span>
            <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">32</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">96</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">160</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">320</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token punctuation">]</span>

        features <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
        <span class="token comment"># conv1 layer</span>
        features<span class="token punctuation">.</span>append<span class="token punctuation">(</span>ConvBNReLU<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> input_channel<span class="token punctuation">,</span> stride<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token comment"># building inverted residual residual blockes</span>
        <span class="token keyword">for</span> t<span class="token punctuation">,</span> c<span class="token punctuation">,</span> n<span class="token punctuation">,</span> s <span class="token keyword">in</span> inverted_residual_setting<span class="token punctuation">:</span>
            output_channel <span class="token operator">=</span> _make_divisible<span class="token punctuation">(</span>c <span class="token operator">*</span> alpha<span class="token punctuation">,</span> round_nearest<span class="token punctuation">)</span>
            <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">:</span>
                stride <span class="token operator">=</span> s <span class="token keyword">if</span> i <span class="token operator">==</span> <span class="token number">0</span> <span class="token keyword">else</span> <span class="token number">1</span>
                features<span class="token punctuation">.</span>append<span class="token punctuation">(</span>block<span class="token punctuation">(</span>input_channel<span class="token punctuation">,</span> output_channel<span class="token punctuation">,</span> stride<span class="token punctuation">,</span> expand_ratio<span class="token operator">=</span>t<span class="token punctuation">)</span><span class="token punctuation">)</span>
                input_channel <span class="token operator">=</span> output_channel
        <span class="token comment"># building last several layers</span>
        features<span class="token punctuation">.</span>append<span class="token punctuation">(</span>ConvBNReLU<span class="token punctuation">(</span>input_channel<span class="token punctuation">,</span> last_channel<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token comment"># combine feature layers</span>
        self<span class="token punctuation">.</span>features <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span><span class="token operator">*</span>features<span class="token punctuation">)</span>

        <span class="token comment"># building classifier</span>
        self<span class="token punctuation">.</span>avgpool <span class="token operator">=</span> nn<span class="token punctuation">.</span>AdaptiveAvgPool2d<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>classifier <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Dropout<span class="token punctuation">(</span><span class="token number">0.2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>last_channel<span class="token punctuation">,</span> num_classes<span class="token punctuation">)</span>
        <span class="token punctuation">)</span>

        <span class="token comment"># weight initialization</span>
        <span class="token keyword">for</span> m <span class="token keyword">in</span> self<span class="token punctuation">.</span>modules<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">)</span><span class="token punctuation">:</span>
                nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>kaiming_normal_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>weight<span class="token punctuation">,</span> mode<span class="token operator">=</span><span class="token string">'fan_out'</span><span class="token punctuation">)</span>
                <span class="token keyword">if</span> m<span class="token punctuation">.</span>bias <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
                    nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>zeros_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>bias<span class="token punctuation">)</span>
            <span class="token keyword">elif</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">)</span><span class="token punctuation">:</span>
                nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>ones_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>weight<span class="token punctuation">)</span>
                nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>zeros_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>bias<span class="token punctuation">)</span>
            <span class="token keyword">elif</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Linear<span class="token punctuation">)</span><span class="token punctuation">:</span>
                nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>normal_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>weight<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0.01</span><span class="token punctuation">)</span>
                nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>zeros_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>bias<span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">:</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>features<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>avgpool<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        x <span class="token operator">=</span> torch<span class="token punctuation">.</span>flatten<span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>classifier<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        <span class="token keyword">return</span> x<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="MobileNet-V3"><a href="#MobileNet-V3" class="headerlink" title="MobileNet V3"></a>MobileNet V3</h2><p>V3网络的亮点：</p>
<ul>
<li>更新Block(bneck)</li>
<li>使用NAS搜索参数</li>
<li>重新设计耗时层结构</li>
</ul>
<p>V3相比V2更准确，更高效</p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221026185125266.png" alt="image-20221026185125266"></p>
<h3 id="V3的Block"><a href="#V3的Block" class="headerlink" title="V3的Block"></a>V3的Block</h3><p>BLOCK相比于V2，增加了以下特性：</p>
<ol>
<li>加入SE模块（引入注意力机制）</li>
<li>更新了激活函数</li>
</ol>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221026185957353.png" alt="image-20221026185957353"></p>
<h3 id="SE模块"><a href="#SE模块" class="headerlink" title="SE模块"></a>SE模块</h3><p>SENet是Squeeze-and-Excitation Networks的简称，拿到了ImageNet2017分类比赛冠军，其效果得到了认可，其提出的SE模块思想简单，易于实现，并且很容易可以加载到现有的网络模型框架中。SENet主要是学习了channel之间的相关性，筛选出了针对通道的注意力，稍微增加了一点计算量，但是效果比较好。</p>
<p><img src="C:\Users\Administrator\Desktop\学习\博客图片\image-20221026203708580.png" alt="image-20221026203708580" style="zoom:67%;"></p>
<p>上图是SE模块的一个例子，我们首先通过平均池化得到每个channel的平均值，接着通过ReLu和H-Sigmoid两个全连接层得到各个channel的权重，原先的特征矩阵乘以得到的权重就实现了注意力机制。具体而言，就是通过学习来自动获取到每个特征通道的重要程度，然后依照这一结果去提升有用的特征并抑制对当前任务用处不大的特征。</p>
<h3 id="重新设计"><a href="#重新设计" class="headerlink" title="重新设计"></a>重新设计</h3><p>重新设计耗时层结构：</p>
<ol>
<li>减少第一个卷积层的卷积核个数(32-&gt;16)</li>
<li>精简Last Stage</li>
</ol>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/image-20221026204402422.png" alt="image-20221026204402422"></p>
<p>重新设计激活函数：</p>
<h3 id="V3结构"><a href="#V3结构" class="headerlink" title="V3结构"></a>V3结构</h3><p>MobileNetV3-Large：</p>
<p><img src="C:\Users\Administrator\Desktop\学习\博客图片\image-20221026205117498.png" alt="image-20221026205117498" style="zoom: 67%;"></p>
<p>MobileNetV3-Small：</p>
<p><img src="C:\Users\Administrator\Desktop\学习\博客图片\image-20221026205139627.png" alt="image-20221026205139627" style="zoom:67%;"></p>
<h3 id="model-1"><a href="#model-1" class="headerlink" title="model"></a>model</h3><p>定义V3基础卷积层</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">ConvBNActivation</span><span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>
                 in_planes<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span>
                 out_planes<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span>
                 kernel_size<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">,</span>
                 stride<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span>
                 groups<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span>
                 norm_layer<span class="token punctuation">:</span> Optional<span class="token punctuation">[</span>Callable<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Module<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">None</span><span class="token punctuation">,</span>
                 activation_layer<span class="token punctuation">:</span> Optional<span class="token punctuation">[</span>Callable<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Module<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">None</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        padding <span class="token operator">=</span> <span class="token punctuation">(</span>kernel_size <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">//</span> <span class="token number">2</span>
        <span class="token keyword">if</span> norm_layer <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
            norm_layer <span class="token operator">=</span> nn<span class="token punctuation">.</span>BatchNorm2d
        <span class="token keyword">if</span> activation_layer <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
            activation_layer <span class="token operator">=</span> nn<span class="token punctuation">.</span>ReLU6
        <span class="token builtin">super</span><span class="token punctuation">(</span>ConvBNActivation<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>in_channels<span class="token operator">=</span>in_planes<span class="token punctuation">,</span>
                                                         out_channels<span class="token operator">=</span>out_planes<span class="token punctuation">,</span>
                                                         kernel_size<span class="token operator">=</span>kernel_size<span class="token punctuation">,</span>
                                                         stride<span class="token operator">=</span>stride<span class="token punctuation">,</span>
                                                         padding<span class="token operator">=</span>padding<span class="token punctuation">,</span>
                                                         groups<span class="token operator">=</span>groups<span class="token punctuation">,</span>
                                                         bias<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                               norm_layer<span class="token punctuation">(</span>out_planes<span class="token punctuation">)</span><span class="token punctuation">,</span>
                                               activation_layer<span class="token punctuation">(</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>定义SE模块</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">SqueezeExcitation</span><span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Module<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> input_c<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span> squeeze_factor<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token builtin">super</span><span class="token punctuation">(</span>SqueezeExcitation<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        squeeze_c <span class="token operator">=</span> _make_divisible<span class="token punctuation">(</span>input_c <span class="token operator">//</span> squeeze_factor<span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>fc1 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>input_c<span class="token punctuation">,</span> squeeze_c<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>fc2 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>squeeze_c<span class="token punctuation">,</span> input_c<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">:</span> Tensor<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Tensor<span class="token punctuation">:</span>
        scale <span class="token operator">=</span> F<span class="token punctuation">.</span>adaptive_avg_pool2d<span class="token punctuation">(</span>x<span class="token punctuation">,</span> output_size<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        scale <span class="token operator">=</span> self<span class="token punctuation">.</span>fc1<span class="token punctuation">(</span>scale<span class="token punctuation">)</span>
        scale <span class="token operator">=</span> F<span class="token punctuation">.</span>relu<span class="token punctuation">(</span>scale<span class="token punctuation">,</span> inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
        scale <span class="token operator">=</span> self<span class="token punctuation">.</span>fc2<span class="token punctuation">(</span>scale<span class="token punctuation">)</span>
        scale <span class="token operator">=</span> F<span class="token punctuation">.</span>hardsigmoid<span class="token punctuation">(</span>scale<span class="token punctuation">,</span> inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> scale <span class="token operator">*</span> x<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>定义倒残差结构：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">InvertedResidualConfig</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>
                 input_c<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span>
                 kernel<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span>
                 expanded_c<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span>
                 out_c<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span>
                 use_se<span class="token punctuation">:</span> <span class="token builtin">bool</span><span class="token punctuation">,</span>
                 activation<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span>
                 stride<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span>
                 width_multi<span class="token punctuation">:</span> <span class="token builtin">float</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        self<span class="token punctuation">.</span>input_c <span class="token operator">=</span> self<span class="token punctuation">.</span>adjust_channels<span class="token punctuation">(</span>input_c<span class="token punctuation">,</span> width_multi<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>kernel <span class="token operator">=</span> kernel
        self<span class="token punctuation">.</span>expanded_c <span class="token operator">=</span> self<span class="token punctuation">.</span>adjust_channels<span class="token punctuation">(</span>expanded_c<span class="token punctuation">,</span> width_multi<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>out_c <span class="token operator">=</span> self<span class="token punctuation">.</span>adjust_channels<span class="token punctuation">(</span>out_c<span class="token punctuation">,</span> width_multi<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>use_se <span class="token operator">=</span> use_se
        self<span class="token punctuation">.</span>use_hs <span class="token operator">=</span> activation <span class="token operator">==</span> <span class="token string">"HS"</span>  <span class="token comment"># whether using h-swish activation</span>
        self<span class="token punctuation">.</span>stride <span class="token operator">=</span> stride

    <span class="token decorator annotation punctuation">@staticmethod</span>
    <span class="token keyword">def</span> <span class="token function">adjust_channels</span><span class="token punctuation">(</span>channels<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span> width_multi<span class="token punctuation">:</span> <span class="token builtin">float</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> _make_divisible<span class="token punctuation">(</span>channels <span class="token operator">*</span> width_multi<span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span>


<span class="token keyword">class</span> <span class="token class-name">InvertedResidual</span><span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Module<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>
                 cnf<span class="token punctuation">:</span> InvertedResidualConfig<span class="token punctuation">,</span>
                 norm_layer<span class="token punctuation">:</span> Callable<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Module<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token builtin">super</span><span class="token punctuation">(</span>InvertedResidual<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>

        <span class="token keyword">if</span> cnf<span class="token punctuation">.</span>stride <span class="token keyword">not</span> <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
            <span class="token keyword">raise</span> ValueError<span class="token punctuation">(</span><span class="token string">"illegal stride value."</span><span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>use_res_connect <span class="token operator">=</span> <span class="token punctuation">(</span>cnf<span class="token punctuation">.</span>stride <span class="token operator">==</span> <span class="token number">1</span> <span class="token keyword">and</span> cnf<span class="token punctuation">.</span>input_c <span class="token operator">==</span> cnf<span class="token punctuation">.</span>out_c<span class="token punctuation">)</span>

        layers<span class="token punctuation">:</span> List<span class="token punctuation">[</span>nn<span class="token punctuation">.</span>Module<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
        activation_layer <span class="token operator">=</span> nn<span class="token punctuation">.</span>Hardswish <span class="token keyword">if</span> cnf<span class="token punctuation">.</span>use_hs <span class="token keyword">else</span> nn<span class="token punctuation">.</span>ReLU

        <span class="token comment"># expand</span>
        <span class="token keyword">if</span> cnf<span class="token punctuation">.</span>expanded_c <span class="token operator">!=</span> cnf<span class="token punctuation">.</span>input_c<span class="token punctuation">:</span>
            layers<span class="token punctuation">.</span>append<span class="token punctuation">(</span>ConvBNActivation<span class="token punctuation">(</span>cnf<span class="token punctuation">.</span>input_c<span class="token punctuation">,</span>
                                           cnf<span class="token punctuation">.</span>expanded_c<span class="token punctuation">,</span>
                                           kernel_size<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>
                                           norm_layer<span class="token operator">=</span>norm_layer<span class="token punctuation">,</span>
                                           activation_layer<span class="token operator">=</span>activation_layer<span class="token punctuation">)</span><span class="token punctuation">)</span>

        <span class="token comment"># depthwise</span>
        layers<span class="token punctuation">.</span>append<span class="token punctuation">(</span>ConvBNActivation<span class="token punctuation">(</span>cnf<span class="token punctuation">.</span>expanded_c<span class="token punctuation">,</span>
                                       cnf<span class="token punctuation">.</span>expanded_c<span class="token punctuation">,</span>
                                       kernel_size<span class="token operator">=</span>cnf<span class="token punctuation">.</span>kernel<span class="token punctuation">,</span>
                                       stride<span class="token operator">=</span>cnf<span class="token punctuation">.</span>stride<span class="token punctuation">,</span>
                                       groups<span class="token operator">=</span>cnf<span class="token punctuation">.</span>expanded_c<span class="token punctuation">,</span>
                                       norm_layer<span class="token operator">=</span>norm_layer<span class="token punctuation">,</span>
                                       activation_layer<span class="token operator">=</span>activation_layer<span class="token punctuation">)</span><span class="token punctuation">)</span>

        <span class="token keyword">if</span> cnf<span class="token punctuation">.</span>use_se<span class="token punctuation">:</span>
            layers<span class="token punctuation">.</span>append<span class="token punctuation">(</span>SqueezeExcitation<span class="token punctuation">(</span>cnf<span class="token punctuation">.</span>expanded_c<span class="token punctuation">)</span><span class="token punctuation">)</span>

        <span class="token comment"># project</span>
        layers<span class="token punctuation">.</span>append<span class="token punctuation">(</span>ConvBNActivation<span class="token punctuation">(</span>cnf<span class="token punctuation">.</span>expanded_c<span class="token punctuation">,</span>
                                       cnf<span class="token punctuation">.</span>out_c<span class="token punctuation">,</span>
                                       kernel_size<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>
                                       norm_layer<span class="token operator">=</span>norm_layer<span class="token punctuation">,</span>
                                       activation_layer<span class="token operator">=</span>nn<span class="token punctuation">.</span>Identity<span class="token punctuation">)</span><span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>block <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span><span class="token operator">*</span>layers<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>out_channels <span class="token operator">=</span> cnf<span class="token punctuation">.</span>out_c
        self<span class="token punctuation">.</span>is_strided <span class="token operator">=</span> cnf<span class="token punctuation">.</span>stride <span class="token operator">&gt;</span> <span class="token number">1</span>

    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">:</span> Tensor<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Tensor<span class="token punctuation">:</span>
        result <span class="token operator">=</span> self<span class="token punctuation">.</span>block<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        <span class="token keyword">if</span> self<span class="token punctuation">.</span>use_res_connect<span class="token punctuation">:</span>
            result <span class="token operator">+=</span> x

        <span class="token keyword">return</span> result<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>定义模型</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">def</span> <span class="token function">_make_divisible</span><span class="token punctuation">(</span>ch<span class="token punctuation">,</span> divisor<span class="token operator">=</span><span class="token number">8</span><span class="token punctuation">,</span> min_ch<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    This function is taken from the original tf repo.
    It ensures that all layers have a channel number that is divisible by 8
    It can be seen here:
    https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
    """</span>
    <span class="token keyword">if</span> min_ch <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
        min_ch <span class="token operator">=</span> divisor
    new_ch <span class="token operator">=</span> <span class="token builtin">max</span><span class="token punctuation">(</span>min_ch<span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span>ch <span class="token operator">+</span> divisor <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">//</span> divisor <span class="token operator">*</span> divisor<span class="token punctuation">)</span>
    <span class="token comment"># Make sure that round down does not go down by more than 10%.</span>
    <span class="token keyword">if</span> new_ch <span class="token operator">&lt;</span> <span class="token number">0.9</span> <span class="token operator">*</span> ch<span class="token punctuation">:</span>
        new_ch <span class="token operator">+=</span> divisor
    <span class="token keyword">return</span> new_ch

<span class="token keyword">class</span> <span class="token class-name">MobileNetV3</span><span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Module<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>
                 inverted_residual_setting<span class="token punctuation">:</span> List<span class="token punctuation">[</span>InvertedResidualConfig<span class="token punctuation">]</span><span class="token punctuation">,</span>
                 last_channel<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span>
                 num_classes<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">1000</span><span class="token punctuation">,</span>
                 block<span class="token punctuation">:</span> Optional<span class="token punctuation">[</span>Callable<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Module<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">None</span><span class="token punctuation">,</span>
                 norm_layer<span class="token punctuation">:</span> Optional<span class="token punctuation">[</span>Callable<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Module<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">None</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token builtin">super</span><span class="token punctuation">(</span>MobileNetV3<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>

        <span class="token keyword">if</span> <span class="token keyword">not</span> inverted_residual_setting<span class="token punctuation">:</span>
            <span class="token keyword">raise</span> ValueError<span class="token punctuation">(</span><span class="token string">"The inverted_residual_setting should not be empty."</span><span class="token punctuation">)</span>
        <span class="token keyword">elif</span> <span class="token keyword">not</span> <span class="token punctuation">(</span><span class="token builtin">isinstance</span><span class="token punctuation">(</span>inverted_residual_setting<span class="token punctuation">,</span> List<span class="token punctuation">)</span> <span class="token keyword">and</span>
                  <span class="token builtin">all</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token builtin">isinstance</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> InvertedResidualConfig<span class="token punctuation">)</span> <span class="token keyword">for</span> s <span class="token keyword">in</span> inverted_residual_setting<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">raise</span> TypeError<span class="token punctuation">(</span><span class="token string">"The inverted_residual_setting should be List[InvertedResidualConfig]"</span><span class="token punctuation">)</span>

        <span class="token keyword">if</span> block <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
            block <span class="token operator">=</span> InvertedResidual

        <span class="token keyword">if</span> norm_layer <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
            norm_layer <span class="token operator">=</span> partial<span class="token punctuation">(</span>nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">,</span> eps<span class="token operator">=</span><span class="token number">0.001</span><span class="token punctuation">,</span> momentum<span class="token operator">=</span><span class="token number">0.01</span><span class="token punctuation">)</span>

        layers<span class="token punctuation">:</span> List<span class="token punctuation">[</span>nn<span class="token punctuation">.</span>Module<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

        <span class="token comment"># building first layer</span>
        firstconv_output_c <span class="token operator">=</span> inverted_residual_setting<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>input_c
        layers<span class="token punctuation">.</span>append<span class="token punctuation">(</span>ConvBNActivation<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span>
                                       firstconv_output_c<span class="token punctuation">,</span>
                                       kernel_size<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">,</span>
                                       stride<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span>
                                       norm_layer<span class="token operator">=</span>norm_layer<span class="token punctuation">,</span>
                                       activation_layer<span class="token operator">=</span>nn<span class="token punctuation">.</span>Hardswish<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token comment"># building inverted residual blocks</span>
        <span class="token keyword">for</span> cnf <span class="token keyword">in</span> inverted_residual_setting<span class="token punctuation">:</span>
            layers<span class="token punctuation">.</span>append<span class="token punctuation">(</span>block<span class="token punctuation">(</span>cnf<span class="token punctuation">,</span> norm_layer<span class="token punctuation">)</span><span class="token punctuation">)</span>

        <span class="token comment"># building last several layers</span>
        lastconv_input_c <span class="token operator">=</span> inverted_residual_setting<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>out_c
        lastconv_output_c <span class="token operator">=</span> <span class="token number">6</span> <span class="token operator">*</span> lastconv_input_c
        layers<span class="token punctuation">.</span>append<span class="token punctuation">(</span>ConvBNActivation<span class="token punctuation">(</span>lastconv_input_c<span class="token punctuation">,</span>
                                       lastconv_output_c<span class="token punctuation">,</span>
                                       kernel_size<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>
                                       norm_layer<span class="token operator">=</span>norm_layer<span class="token punctuation">,</span>
                                       activation_layer<span class="token operator">=</span>nn<span class="token punctuation">.</span>Hardswish<span class="token punctuation">)</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>features <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span><span class="token operator">*</span>layers<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>avgpool <span class="token operator">=</span> nn<span class="token punctuation">.</span>AdaptiveAvgPool2d<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>classifier <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>lastconv_output_c<span class="token punctuation">,</span> last_channel<span class="token punctuation">)</span><span class="token punctuation">,</span>
                                        nn<span class="token punctuation">.</span>Hardswish<span class="token punctuation">(</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                        nn<span class="token punctuation">.</span>Dropout<span class="token punctuation">(</span>p<span class="token operator">=</span><span class="token number">0.2</span><span class="token punctuation">,</span> inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                                        nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>last_channel<span class="token punctuation">,</span> num_classes<span class="token punctuation">)</span><span class="token punctuation">)</span>

        <span class="token comment"># initial weights</span>
        <span class="token keyword">for</span> m <span class="token keyword">in</span> self<span class="token punctuation">.</span>modules<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">)</span><span class="token punctuation">:</span>
                nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>kaiming_normal_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>weight<span class="token punctuation">,</span> mode<span class="token operator">=</span><span class="token string">"fan_out"</span><span class="token punctuation">)</span>
                <span class="token keyword">if</span> m<span class="token punctuation">.</span>bias <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
                    nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>zeros_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>bias<span class="token punctuation">)</span>
            <span class="token keyword">elif</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> <span class="token punctuation">(</span>nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">,</span> nn<span class="token punctuation">.</span>GroupNorm<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>ones_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>weight<span class="token punctuation">)</span>
                nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>zeros_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>bias<span class="token punctuation">)</span>
            <span class="token keyword">elif</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Linear<span class="token punctuation">)</span><span class="token punctuation">:</span>
                nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>normal_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>weight<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0.01</span><span class="token punctuation">)</span>
                nn<span class="token punctuation">.</span>init<span class="token punctuation">.</span>zeros_<span class="token punctuation">(</span>m<span class="token punctuation">.</span>bias<span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">_forward_impl</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">:</span> Tensor<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Tensor<span class="token punctuation">:</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>features<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>avgpool<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        x <span class="token operator">=</span> torch<span class="token punctuation">.</span>flatten<span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>classifier<span class="token punctuation">(</span>x<span class="token punctuation">)</span>

        <span class="token keyword">return</span> x

    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">:</span> Tensor<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Tensor<span class="token punctuation">:</span>
        <span class="token keyword">return</span> self<span class="token punctuation">.</span>_forward_impl<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>实例化MobileNetV3-Large</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">def</span> <span class="token function">mobilenet_v3_large</span><span class="token punctuation">(</span>num_classes<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">1000</span><span class="token punctuation">,</span>
                       reduced_tail<span class="token punctuation">:</span> <span class="token builtin">bool</span> <span class="token operator">=</span> <span class="token boolean">False</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> MobileNetV3<span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    Constructs a large MobileNetV3 architecture from
    "Searching for MobileNetV3" &lt;https://arxiv.org/abs/1905.02244&gt;.

    weights_link:
    https://download.pytorch.org/models/mobilenet_v3_large-8738ca79.pth

    Args:
        num_classes (int): number of classes
        reduced_tail (bool): If True, reduces the channel counts of all feature layers
            between C4 and C5 by 2. It is used to reduce the channel redundancy in the
            backbone for Detection and Segmentation.
    """</span>
    width_multi <span class="token operator">=</span> <span class="token number">1.0</span>
    bneck_conf <span class="token operator">=</span> partial<span class="token punctuation">(</span>InvertedResidualConfig<span class="token punctuation">,</span> width_multi<span class="token operator">=</span>width_multi<span class="token punctuation">)</span>
    adjust_channels <span class="token operator">=</span> partial<span class="token punctuation">(</span>InvertedResidualConfig<span class="token punctuation">.</span>adjust_channels<span class="token punctuation">,</span> width_multi<span class="token operator">=</span>width_multi<span class="token punctuation">)</span>

    reduce_divider <span class="token operator">=</span> <span class="token number">2</span> <span class="token keyword">if</span> reduced_tail <span class="token keyword">else</span> <span class="token number">1</span>

    inverted_residual_setting <span class="token operator">=</span> <span class="token punctuation">[</span>
        <span class="token comment"># input_c, kernel, expanded_c, out_c, use_se, activation, stride</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token string">"RE"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token number">24</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token string">"RE"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>  <span class="token comment"># C1</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">72</span><span class="token punctuation">,</span> <span class="token number">24</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token string">"RE"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">72</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"RE"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>  <span class="token comment"># C2</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">120</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"RE"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">120</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"RE"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">240</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>  <span class="token comment"># C3</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">184</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">184</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">480</span><span class="token punctuation">,</span> <span class="token number">112</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">112</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">672</span><span class="token punctuation">,</span> <span class="token number">112</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">112</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">672</span><span class="token punctuation">,</span> <span class="token number">160</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>  <span class="token comment"># C4</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">160</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">960</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token number">160</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">160</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">960</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token number">160</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span>
    last_channel <span class="token operator">=</span> adjust_channels<span class="token punctuation">(</span><span class="token number">1280</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">)</span>  <span class="token comment"># C5</span>

    <span class="token keyword">return</span> MobileNetV3<span class="token punctuation">(</span>inverted_residual_setting<span class="token operator">=</span>inverted_residual_setting<span class="token punctuation">,</span>
                       last_channel<span class="token operator">=</span>last_channel<span class="token punctuation">,</span>
                       num_classes<span class="token operator">=</span>num_classes<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>实例化MobileNetV3-Small</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">def</span> <span class="token function">mobilenet_v3_small</span><span class="token punctuation">(</span>num_classes<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">1000</span><span class="token punctuation">,</span>
                       reduced_tail<span class="token punctuation">:</span> <span class="token builtin">bool</span> <span class="token operator">=</span> <span class="token boolean">False</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> MobileNetV3<span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    Constructs a large MobileNetV3 architecture from
    "Searching for MobileNetV3" &lt;https://arxiv.org/abs/1905.02244&gt;.

    weights_link:
    https://download.pytorch.org/models/mobilenet_v3_small-047dcff4.pth

    Args:
        num_classes (int): number of classes
        reduced_tail (bool): If True, reduces the channel counts of all feature layers
            between C4 and C5 by 2. It is used to reduce the channel redundancy in the
            backbone for Detection and Segmentation.
    """</span>
    width_multi <span class="token operator">=</span> <span class="token number">1.0</span>
    bneck_conf <span class="token operator">=</span> partial<span class="token punctuation">(</span>InvertedResidualConfig<span class="token punctuation">,</span> width_multi<span class="token operator">=</span>width_multi<span class="token punctuation">)</span>
    adjust_channels <span class="token operator">=</span> partial<span class="token punctuation">(</span>InvertedResidualConfig<span class="token punctuation">.</span>adjust_channels<span class="token punctuation">,</span> width_multi<span class="token operator">=</span>width_multi<span class="token punctuation">)</span>

    reduce_divider <span class="token operator">=</span> <span class="token number">2</span> <span class="token keyword">if</span> reduced_tail <span class="token keyword">else</span> <span class="token number">1</span>

    inverted_residual_setting <span class="token operator">=</span> <span class="token punctuation">[</span>
        <span class="token comment"># input_c, kernel, expanded_c, out_c, use_se, activation, stride</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"RE"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>  <span class="token comment"># C1</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">72</span><span class="token punctuation">,</span> <span class="token number">24</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token string">"RE"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>  <span class="token comment"># C2</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">88</span><span class="token punctuation">,</span> <span class="token number">24</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token string">"RE"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">96</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>  <span class="token comment"># C3</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">240</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">240</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">120</span><span class="token punctuation">,</span> <span class="token number">48</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">48</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">144</span><span class="token punctuation">,</span> <span class="token number">48</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">48</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">288</span><span class="token punctuation">,</span> <span class="token number">96</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>  <span class="token comment"># C4</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">96</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">576</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token number">96</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        bneck_conf<span class="token punctuation">(</span><span class="token number">96</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">576</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token number">96</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"HS"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token punctuation">]</span>
    last_channel <span class="token operator">=</span> adjust_channels<span class="token punctuation">(</span><span class="token number">1024</span> <span class="token operator">//</span> reduce_divider<span class="token punctuation">)</span>  <span class="token comment"># C5</span>

    <span class="token keyword">return</span> MobileNetV3<span class="token punctuation">(</span>inverted_residual_setting<span class="token operator">=</span>inverted_residual_setting<span class="token punctuation">,</span>
                       last_channel<span class="token operator">=</span>last_channel<span class="token punctuation">,</span>
                       num_classes<span class="token operator">=</span>num_classes<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h1 id="ShuffleNet"><a href="#ShuffleNet" class="headerlink" title="ShuffleNet"></a>ShuffleNet</h1><h2 id="ShuffleNet-V1"><a href="#ShuffleNet-V1" class="headerlink" title="ShuffleNet V1"></a>ShuffleNet V1</h2><p>ShuffleNet是一种专门为计算资源有限的设备设计的神经网络结构，主要采用了pointwise group convolution 和 channel shuffle两种技术，在保留了模型精度的同时极大减少了计算开销。</p>
<h3 id="channel-shuffle"><a href="#channel-shuffle" class="headerlink" title="channel shuffle"></a>channel shuffle</h3><p>ShuffleNet的核心设计理念是对不同的channels进行shuffle来解决group convolution带来的弊端。之前我们介绍Renext的时候它的核心思想就是将channel分组来降低计算量，但是group convolution存在另外一个弊端：如图1-a所示，其中GConv是group convolution，这里分组数是3。可以看到当堆积GConv层后一个问题是不同组之间的特征图是不通信的，这就好像分了三个互不相干的路，大家各走各的，这目测会降低网络的特征提取能力。这也是为什么之前的网络在group convolution后采用密集的1x1卷积，因为要保证group convolution之后不同组的特征图之间的信息交流。</p>
<p>ShuffleNet参照了group convolution的思想并在其基础上做出改进，如图1-b所示，你可以对group convolution之后的特征图进行“重组”，这样可以保证接下了采用的group convolution其输入来自不同的组，因此信息可以在不同组之间流转。这个操作等价于图2-c，即group convolution之后对channels进行shuffle，但并不是随机的，其实是“均匀地打乱”。</p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/v2-f8ddc33fb1f578bf7f51a4b6e5407426_r.jpg" alt="图1"></p>
<h3 id="网络结构"><a href="#网络结构" class="headerlink" title="网络结构"></a>网络结构</h3><p>ShuffleNet的基本单元是在一个残差单元的基础上改进而成的。如图2-a所示，这是一个包含3层的残差单元：首先是1x1卷积，然后是3x3的depthwise convolution（DWConv，主要是为了降低计算量），这里的3x3卷积是瓶颈层（bottleneck），紧接着是1x1卷积，最后是一个短路连接，将输入直接加到输出上。现在，进行如下的改进：将密集的1x1卷积替换成1x1的group convolution，不过在第一个1x1卷积之后增加了一个channel shuffle操作。值得注意的是3x3卷积后面没有增加channel shuffle，按paper的意思，对于这样一个残差单元，一个channel shuffle操作是足够了。还有就是3x3的depthwise convolution之后没有使用ReLU激活函数。改进之后如图2-b所示。对于残差单元，如果stride=1时，此时输入与输出shape一致可以直接相加，而当stride=2时，通道数增加，而特征图大小减小，此时输入与输出不匹配。一般情况下可以采用一个1x1卷积将输入映射成和输出一样的shape。但是在ShuffleNet中，却采用了不一样的策略，如图2-c所示：对原输入采用stride=2的3x3 avg pool，这样得到和输出一样大小的特征图，然后将得到特征图与输出进行连接（concat），而不是相加。这样做的目的主要是降低计算量与参数大小。</p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/v2-a8d5297a16f7bcc40a31d427cf062e58_720w.webp" alt="图2"></p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/v2-3331c0c1803d7289ee87b575b020a68b_720w.webp" alt="img"></p>
<h2 id="ShuffleNet-V2"><a href="#ShuffleNet-V2" class="headerlink" title="ShuffleNet V2"></a>ShuffleNet V2</h2><h3 id="网络结构-1"><a href="#网络结构-1" class="headerlink" title="网络结构"></a>网络结构</h3><p>ShuffleNetV2对shufflenet进行了进一步的改进，并且提出了四个设计轻量化网络的原则：</p>
<ol>
<li>当卷积层的输入特征矩阵与输出特征矩阵channel相等时内存访问量MAC最小(保持FLOPs不变时)</li>
<li>当GConv的groups增大时(保持FLOPs不变时)，MAC也会增大</li>
<li>网络设计的碎片化程度越高（复杂的并行串行结构），速度越慢</li>
<li>Element-wise操作带来的影响是不可忽视的（如Relu、AddTensor等）</li>
</ol>
<p>我们基于上述四个原则对V1的网络结构进行优化（左边两个为V1，右边两个为V2优化过后的结构）</p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/732da3042e49458f8fdf6f9a75f98a40.png" alt="V1-V2"></p>
<p>如图( c )所示，在每个单元的开始，c特征通道的输入被分为两个分支（在ShuffleNetV2中这里是对channels均分成两半）。根据G3，不能使用太多的分支，所以其中一个分支不作改变，另外的一个分支由三个卷积组成，它们具有相同的输入和输出通道以满足G1。两个1 × 1卷积不再是组卷积，而改变为普通的1x1卷积操作，这是为了遵循G2（需要考虑组的代价）。卷积后，两个分支被连接起来，而不是相加(G4)。因此，通道的数量保持不变(G1)。然后使用与ShuffleNetV1中相同的“channels shuffle”操作来启用两个分支之间的信息通信。需要注意，ShuffleNet v1中的“Add”操作不再存在。像ReLU和depthwise convolutions 这样的元素操作只存在于一个分支中。</p>
<p><img src="https://pluto-1300780100.cos.ap-nanjing.myqcloud.com/img/20210624175820178.png" alt="V2整体网络结构"></p>
<h3 id="model-2"><a href="#model-2" class="headerlink" title="model"></a>model</h3><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> typing <span class="token keyword">import</span> List<span class="token punctuation">,</span> Callable

<span class="token keyword">import</span> torch
<span class="token keyword">from</span> torch <span class="token keyword">import</span> Tensor
<span class="token keyword">import</span> torch<span class="token punctuation">.</span>nn <span class="token keyword">as</span> nn


<span class="token keyword">def</span> <span class="token function">channel_shuffle</span><span class="token punctuation">(</span>x<span class="token punctuation">:</span> Tensor<span class="token punctuation">,</span> groups<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Tensor<span class="token punctuation">:</span>

    batch_size<span class="token punctuation">,</span> num_channels<span class="token punctuation">,</span> height<span class="token punctuation">,</span> width <span class="token operator">=</span> x<span class="token punctuation">.</span>size<span class="token punctuation">(</span><span class="token punctuation">)</span>
    channels_per_group <span class="token operator">=</span> num_channels <span class="token operator">//</span> groups

    <span class="token comment"># reshape</span>
    <span class="token comment"># [batch_size, num_channels, height, width] -&gt; [batch_size, groups, channels_per_group, height, width]</span>
    x <span class="token operator">=</span> x<span class="token punctuation">.</span>view<span class="token punctuation">(</span>batch_size<span class="token punctuation">,</span> groups<span class="token punctuation">,</span> channels_per_group<span class="token punctuation">,</span> height<span class="token punctuation">,</span> width<span class="token punctuation">)</span>

    x <span class="token operator">=</span> torch<span class="token punctuation">.</span>transpose<span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span>contiguous<span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token comment"># flatten</span>
    x <span class="token operator">=</span> x<span class="token punctuation">.</span>view<span class="token punctuation">(</span>batch_size<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> height<span class="token punctuation">,</span> width<span class="token punctuation">)</span>

    <span class="token keyword">return</span> x


<span class="token keyword">class</span> <span class="token class-name">InvertedResidual</span><span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Module<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> input_c<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span> output_c<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span> stride<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token builtin">super</span><span class="token punctuation">(</span>InvertedResidual<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>

        <span class="token keyword">if</span> stride <span class="token keyword">not</span> <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
            <span class="token keyword">raise</span> ValueError<span class="token punctuation">(</span><span class="token string">"illegal stride value."</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>stride <span class="token operator">=</span> stride

        <span class="token keyword">assert</span> output_c <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span>
        branch_features <span class="token operator">=</span> output_c <span class="token operator">//</span> <span class="token number">2</span>
        <span class="token comment"># 当stride为1时，input_channel应该是branch_features的两倍</span>
        <span class="token comment"># python中 '&lt;&lt;' 是位运算，可理解为计算×2的快速方法</span>
        <span class="token keyword">assert</span> <span class="token punctuation">(</span>self<span class="token punctuation">.</span>stride <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">or</span> <span class="token punctuation">(</span>input_c <span class="token operator">==</span> branch_features <span class="token operator">&lt;&lt;</span> <span class="token number">1</span><span class="token punctuation">)</span>

        <span class="token keyword">if</span> self<span class="token punctuation">.</span>stride <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">:</span>
            self<span class="token punctuation">.</span>branch1 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
                self<span class="token punctuation">.</span>depthwise_conv<span class="token punctuation">(</span>input_c<span class="token punctuation">,</span> input_c<span class="token punctuation">,</span> kernel_s<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">,</span> stride<span class="token operator">=</span>self<span class="token punctuation">.</span>stride<span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">(</span>input_c<span class="token punctuation">)</span><span class="token punctuation">,</span>
                nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>input_c<span class="token punctuation">,</span> branch_features<span class="token punctuation">,</span> kernel_size<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> stride<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> bias<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">(</span>branch_features<span class="token punctuation">)</span><span class="token punctuation">,</span>
                nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
            <span class="token punctuation">)</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            self<span class="token punctuation">.</span>branch1 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span><span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>branch2 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>input_c <span class="token keyword">if</span> self<span class="token punctuation">.</span>stride <span class="token operator">&gt;</span> <span class="token number">1</span> <span class="token keyword">else</span> branch_features<span class="token punctuation">,</span> branch_features<span class="token punctuation">,</span> kernel_size<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>
                      stride<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> bias<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">(</span>branch_features<span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            self<span class="token punctuation">.</span>depthwise_conv<span class="token punctuation">(</span>branch_features<span class="token punctuation">,</span> branch_features<span class="token punctuation">,</span> kernel_s<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">,</span> stride<span class="token operator">=</span>self<span class="token punctuation">.</span>stride<span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">(</span>branch_features<span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>branch_features<span class="token punctuation">,</span> branch_features<span class="token punctuation">,</span> kernel_size<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> stride<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> bias<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">(</span>branch_features<span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span>

    <span class="token decorator annotation punctuation">@staticmethod</span>
    <span class="token keyword">def</span> <span class="token function">depthwise_conv</span><span class="token punctuation">(</span>input_c<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span>
                       output_c<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span>
                       kernel_s<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">,</span>
                       stride<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span>
                       padding<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span>
                       bias<span class="token punctuation">:</span> <span class="token builtin">bool</span> <span class="token operator">=</span> <span class="token boolean">False</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">:</span>
        <span class="token keyword">return</span> nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>in_channels<span class="token operator">=</span>input_c<span class="token punctuation">,</span> out_channels<span class="token operator">=</span>output_c<span class="token punctuation">,</span> kernel_size<span class="token operator">=</span>kernel_s<span class="token punctuation">,</span>
                         stride<span class="token operator">=</span>stride<span class="token punctuation">,</span> padding<span class="token operator">=</span>padding<span class="token punctuation">,</span> bias<span class="token operator">=</span>bias<span class="token punctuation">,</span> groups<span class="token operator">=</span>input_c<span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">:</span> Tensor<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Tensor<span class="token punctuation">:</span>
        <span class="token keyword">if</span> self<span class="token punctuation">.</span>stride <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>
            x1<span class="token punctuation">,</span> x2 <span class="token operator">=</span> x<span class="token punctuation">.</span>chunk<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> dim<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
            out <span class="token operator">=</span> torch<span class="token punctuation">.</span>cat<span class="token punctuation">(</span><span class="token punctuation">(</span>x1<span class="token punctuation">,</span> self<span class="token punctuation">.</span>branch2<span class="token punctuation">(</span>x2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> dim<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            out <span class="token operator">=</span> torch<span class="token punctuation">.</span>cat<span class="token punctuation">(</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>branch1<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">,</span> self<span class="token punctuation">.</span>branch2<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> dim<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>

        out <span class="token operator">=</span> channel_shuffle<span class="token punctuation">(</span>out<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>

        <span class="token keyword">return</span> out


<span class="token keyword">class</span> <span class="token class-name">ShuffleNetV2</span><span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Module<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>
                 stages_repeats<span class="token punctuation">:</span> List<span class="token punctuation">[</span><span class="token builtin">int</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                 stages_out_channels<span class="token punctuation">:</span> List<span class="token punctuation">[</span><span class="token builtin">int</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                 num_classes<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">1000</span><span class="token punctuation">,</span>
                 inverted_residual<span class="token punctuation">:</span> Callable<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Module<span class="token punctuation">]</span> <span class="token operator">=</span> InvertedResidual<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token builtin">super</span><span class="token punctuation">(</span>ShuffleNetV2<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>

        <span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>stages_repeats<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">3</span><span class="token punctuation">:</span>
            <span class="token keyword">raise</span> ValueError<span class="token punctuation">(</span><span class="token string">"expected stages_repeats as list of 3 positive ints"</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>stages_out_channels<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">5</span><span class="token punctuation">:</span>
            <span class="token keyword">raise</span> ValueError<span class="token punctuation">(</span><span class="token string">"expected stages_out_channels as list of 5 positive ints"</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>_stage_out_channels <span class="token operator">=</span> stages_out_channels

        <span class="token comment"># input RGB image</span>
        input_channels <span class="token operator">=</span> <span class="token number">3</span>
        output_channels <span class="token operator">=</span> self<span class="token punctuation">.</span>_stage_out_channels<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>

        self<span class="token punctuation">.</span>conv1 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>input_channels<span class="token punctuation">,</span> output_channels<span class="token punctuation">,</span> kernel_size<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">,</span> stride<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> bias<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">(</span>output_channels<span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span>
        input_channels <span class="token operator">=</span> output_channels

        self<span class="token punctuation">.</span>maxpool <span class="token operator">=</span> nn<span class="token punctuation">.</span>MaxPool2d<span class="token punctuation">(</span>kernel_size<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">,</span> stride<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>

        <span class="token comment"># Static annotations for mypy</span>
        self<span class="token punctuation">.</span>stage2<span class="token punctuation">:</span> nn<span class="token punctuation">.</span>Sequential
        self<span class="token punctuation">.</span>stage3<span class="token punctuation">:</span> nn<span class="token punctuation">.</span>Sequential
        self<span class="token punctuation">.</span>stage4<span class="token punctuation">:</span> nn<span class="token punctuation">.</span>Sequential

        stage_names <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"stage{}"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
        <span class="token keyword">for</span> name<span class="token punctuation">,</span> repeats<span class="token punctuation">,</span> output_channels <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>stage_names<span class="token punctuation">,</span> stages_repeats<span class="token punctuation">,</span>
                                                  self<span class="token punctuation">.</span>_stage_out_channels<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            seq <span class="token operator">=</span> <span class="token punctuation">[</span>inverted_residual<span class="token punctuation">(</span>input_channels<span class="token punctuation">,</span> output_channels<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
            <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>repeats <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                seq<span class="token punctuation">.</span>append<span class="token punctuation">(</span>inverted_residual<span class="token punctuation">(</span>output_channels<span class="token punctuation">,</span> output_channels<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token builtin">setattr</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> name<span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span><span class="token operator">*</span>seq<span class="token punctuation">)</span><span class="token punctuation">)</span>
            input_channels <span class="token operator">=</span> output_channels

        output_channels <span class="token operator">=</span> self<span class="token punctuation">.</span>_stage_out_channels<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
        self<span class="token punctuation">.</span>conv5 <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span>
            nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>input_channels<span class="token punctuation">,</span> output_channels<span class="token punctuation">,</span> kernel_size<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> stride<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> bias<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">(</span>output_channels<span class="token punctuation">)</span><span class="token punctuation">,</span>
            nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>fc <span class="token operator">=</span> nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span>output_channels<span class="token punctuation">,</span> num_classes<span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">_forward_impl</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">:</span> Tensor<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Tensor<span class="token punctuation">:</span>
        <span class="token comment"># See note [TorchScript super()]</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>conv1<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>maxpool<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>stage2<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>stage3<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>stage4<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>conv5<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        x <span class="token operator">=</span> x<span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># global pool</span>
        x <span class="token operator">=</span> self<span class="token punctuation">.</span>fc<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
        <span class="token keyword">return</span> x

    <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">:</span> Tensor<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Tensor<span class="token punctuation">:</span>
        <span class="token keyword">return</span> self<span class="token punctuation">.</span>_forward_impl<span class="token punctuation">(</span>x<span class="token punctuation">)</span>


<span class="token keyword">def</span> <span class="token function">shufflenet_v2_x0_5</span><span class="token punctuation">(</span>num_classes<span class="token operator">=</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    Constructs a ShuffleNetV2 with 0.5x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    &lt;https://arxiv.org/abs/1807.11164&gt;`.
    weight: https://download.pytorch.org/models/shufflenetv2_x0.5-f707e7126e.pth

    :param num_classes:
    :return:
    """</span>
    model <span class="token operator">=</span> ShuffleNetV2<span class="token punctuation">(</span>stages_repeats<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                         stages_out_channels<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">48</span><span class="token punctuation">,</span> <span class="token number">96</span><span class="token punctuation">,</span> <span class="token number">192</span><span class="token punctuation">,</span> <span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                         num_classes<span class="token operator">=</span>num_classes<span class="token punctuation">)</span>

    <span class="token keyword">return</span> model


<span class="token keyword">def</span> <span class="token function">shufflenet_v2_x1_0</span><span class="token punctuation">(</span>num_classes<span class="token operator">=</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    Constructs a ShuffleNetV2 with 1.0x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    &lt;https://arxiv.org/abs/1807.11164&gt;`.
    weight: https://download.pytorch.org/models/shufflenetv2_x1-5666bf0f80.pth

    :param num_classes:
    :return:
    """</span>
    model <span class="token operator">=</span> ShuffleNetV2<span class="token punctuation">(</span>stages_repeats<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                         stages_out_channels<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">116</span><span class="token punctuation">,</span> <span class="token number">232</span><span class="token punctuation">,</span> <span class="token number">464</span><span class="token punctuation">,</span> <span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                         num_classes<span class="token operator">=</span>num_classes<span class="token punctuation">)</span>

    <span class="token keyword">return</span> model


<span class="token keyword">def</span> <span class="token function">shufflenet_v2_x1_5</span><span class="token punctuation">(</span>num_classes<span class="token operator">=</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    Constructs a ShuffleNetV2 with 1.0x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    &lt;https://arxiv.org/abs/1807.11164&gt;`.
    weight: https://download.pytorch.org/models/shufflenetv2_x1_5-3c479a10.pth

    :param num_classes:
    :return:
    """</span>
    model <span class="token operator">=</span> ShuffleNetV2<span class="token punctuation">(</span>stages_repeats<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                         stages_out_channels<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">176</span><span class="token punctuation">,</span> <span class="token number">352</span><span class="token punctuation">,</span> <span class="token number">704</span><span class="token punctuation">,</span> <span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                         num_classes<span class="token operator">=</span>num_classes<span class="token punctuation">)</span>

    <span class="token keyword">return</span> model


<span class="token keyword">def</span> <span class="token function">shufflenet_v2_x2_0</span><span class="token punctuation">(</span>num_classes<span class="token operator">=</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    Constructs a ShuffleNetV2 with 1.0x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    &lt;https://arxiv.org/abs/1807.11164&gt;`.
    weight: https://download.pytorch.org/models/shufflenetv2_x2_0-8be3c8ee.pth

    :param num_classes:
    :return:
    """</span>
    model <span class="token operator">=</span> ShuffleNetV2<span class="token punctuation">(</span>stages_repeats<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                         stages_out_channels<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">244</span><span class="token punctuation">,</span> <span class="token number">488</span><span class="token punctuation">,</span> <span class="token number">976</span><span class="token punctuation">,</span> <span class="token number">2048</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                         num_classes<span class="token operator">=</span>num_classes<span class="token punctuation">)</span>

    <span class="token keyword">return</span> model<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h1 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h1><p>英文论文对我来说还真是一道不可逾越的大山😭</p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">J Sir</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://jy741.gitee.io/2022/10/28/shen-du-xue-xi-tu-xiang-fen-lei-ru-men-partii/">https://jy741.gitee.io/2022/10/28/shen-du-xue-xi-tu-xiang-fen-lei-ru-men-partii/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">J Sir</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/python/">
                                    <span class="chip bg-color">python</span>
                                </a>
                            
                                <a href="/tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">
                                    <span class="chip bg-color">深度学习</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2022/10/31/zi-zhu-yi-li-ji-zhi-self-attention/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/5.jpg" class="responsive-img" alt="自注意力机制Self-Attention">
                        
                        <span class="card-title">自注意力机制Self-Attention</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            EverydayOneCat稍稍2 🌈

                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-10-31
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            J Sir
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">
                        <span class="chip bg-color">机器学习</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2022/10/19/shen-du-xue-xi-tu-xiang-fen-lei-ru-men-parti/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/11.jpg" class="responsive-img" alt="深度学习——图像分类入门PartⅠ">
                        
                        <span class="card-title">深度学习——图像分类入门PartⅠ</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            EverydayOneCat⬆️??!??!⬆️ 😯😯⁉️ 🐯⬇️

                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-10-19
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            J Sir
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/python/">
                        <span class="chip bg-color">python</span>
                    </a>
                    
                    <a href="/tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">
                        <span class="chip bg-color">深度学习</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h1, h2, h3, h4, h5'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>


<script src="https://cdn.bootcss.com/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
    MathJax.Hub.Config({
        tex2jax: {inlineMath: [['$', '$'], ['\(', '\)']]}
    });
</script>



    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="503838841"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2020-2023</span>
            
            <span id="year">2020</span>
            <a href="/about" target="_blank">J Sir</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">300.9k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">


    <a href="mailto:2065373132@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2065373132" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2065373132" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

	
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"live2d-widget-model-hibiki"},"display":{"position":"right","width":145,"height":315},"mobile":{"show":true,"scale":0.5},"react":{"opacityDefault":0.7,"opacityOnHover":0.8},"log":false});</script></body>

</html>
